﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using appInterfaces;
using System.Messaging;
using appDataContracts;
using appClient;
using appInterfaces;
using System.ServiceModel;

namespace appClient
{
	public partial class About : System.Web.UI.Page
	{

		IClient client;
		public int productIdGet;
		public int catalogIdGet;

		protected void Page_Load(object sender, EventArgs e)
		{

			client = new ChannelFactory<IClient>("clientHttp").CreateChannel();

			if (Request.QueryString["p"] != null && Request.QueryString["c"] != null  && Session["user"] != null)
			{
				productIdGet = int.Parse(Request.QueryString["p"]);
				catalogIdGet = int.Parse(Request.QueryString["c"]);

				FirstName.Text = ((SerializedClient)Session["user"]).CLIENTFIRSTNAME;
				LastName.Text = ((SerializedClient)Session["user"]).CLIENTLASTNAME;
				Mail.Text = ((SerializedClient)Session["user"]).CLIENTMAIL;
			}
			else
				Response.Redirect("Account/Login.aspx");
		}

		public void sendMsg(SerializedClient cli, SerializedSupplier supplier, string mess)
		{
			//MsmqContractClient client = new MsmqContractClient("MsmqService");
			//client.sendMessage("Alex", "Kikoo");

			// create a message queue object
			MessageQueue MQueue = new MessageQueue("a-pc\\private$\\msmqservice/msmqservice.svc");

			//
			SerializedMessage msg = new SerializedMessage(cli,
															supplier,
															mess);
			// create the message and set the base properties
			Message Msg = new Message(msg);
			//Msg.ResponseQueue = new MessageQueue(ResponseMessageQueuePath);
			Msg.Priority = MessagePriority.Normal;
			Msg.UseJournalQueue = true;
			Msg.Label = client.getProductInfo(productIdGet).PRODUCTNAME;

			// we want a acknowledgement if received or not in the response queue
			Msg.AcknowledgeType = AcknowledgeTypes.FullReceive;
			// Msg.AdministrationQueue = new MessageQueue(ResponseMessageQueuePath);

			// send the message
			MQueue.Send(Msg);

			// close the mesage queue
			MQueue.Close();
		}

		protected void SumbitMail_Click(object sender, EventArgs e)
		{
			//send
			SerializedSupplier supp = client.getSupplierByCatalog(catalogIdGet);
			sendMsg((SerializedClient)Session["user"], supp, messageTextArea.InnerText);
			Response.Redirect("Default.aspx");
		}
	}
}
